import { createID } from "../../core/utils/utils";
import { People } from "../people";

const _id = createID();

export class Player extends People{
  constructor(...params){
    super(...params)

		// 生灵正在进行的操作
		this.control = null;
  }

  /**
   * 修改地区定位
   * @param {*} pos 
   */
  setMapPos(pos){
    if(this.control){
      this.root.message('alert', `你的<${this.control.sideHustle.text}>被取消`);
      this.control = null;
    }

    super.setMapPos(pos);
    this.root.savePlayerData();
  }

  /**
   * 修改世界定位
   * @param {*} pos 
   */
  setWorldPos(pos){
    if(this.control){
      this.root.message('alert', `你的<${this.control.sideHustle.text}>被取消`);
      this.control = null;
    }

    super.setWorldPos(pos);
  }

  // 设置操作
  setControl(good, data){
    if(!data.sideHustle.effect){
      this.root.message('alert', `你的<${data.sideHustle.text}>等级不足`);
      return;
    }

    let control = this.control;
    let result = {
      good, ...data, id: _id()
    }

    if(control){
      if (good == control.good && control.text == data.text) {
        this.root.message("alert", "重复的操作");
        return;
      }

      let { cancel } = this.root.gameState.dialogs.showWarning(
				"警告",
				`你正在对<${control.good.getTitle()}>执行<${control.text}>操作`,
				[`cancel:bad:覆盖上次操作`, "ok:success:放弃本次操作"]
			);

      cancel.add(() => {
        this.control = result;
      })
    } else {
      this.control = result;
    }
  }

  update(){
    let control = this.control;
    if(control){
      this.root.time.run(`player-control-${control.id}`, () => {
        control.fn.call(control.good);
        this.control = null;
      }, control.sideHustle.time, true, true);
    }
  }
}